#include "VTranslate.h"
#include "VGlobal.h"
#include "VFunctions.h"

#include <QDebug>

QChar VTranslate::s63_to_qchar(QString s)
{
    if (s.length() != 3)
    {
        return -1;
    }

    static char re_key[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 17, 18, 32, 10, 54, 55, 42, 43, 22, 62, 0, 0, 0, 0, 0, 0,
        0, 34, 30, 35, 38, 52, 27, 9, 2, 53, 47, 48, 13, 14, 39, 58, 59, 25, 26,
        29, 31, 44, 28, 60, 3, 4, 37, 0, 0, 0, 0, 40, 0, 46, 49, 0, 1, 36, 5, 6,
        7, 8, 50, 41, 11, 12, 33, 61, 15, 16, 51, 23, 24, 45, 56, 57, 21, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0 };

    int w1 = s.at(0).unicode();
    int w2 = s.at(1).unicode();
    int w3 = s.at(2).unicode();

    if (w1 > 255 || w2 > 255 || w3 > 255)
    {
        return -1;
    }

    return QChar(re_key[w1] * 63 * 63 + re_key[w2] * 63 + re_key[w3]);
}

QString VTranslate::dec_to_s63(uint n)
{
    if (n > 63 * 63 * 63)
    {
        qDebug() << "result";
        return QString();
    }

    static char key[63] = { 'c', 'd', 'H', 'X', 'Y', 'f', 'g', 'h', 'i', 'G', '3', 'l', 'm', 'L', 'M',
                    'p', 'q', '0', '1', 'y', 'z', 'x', '8', 's', 't', 'Q', 'R', 'F', 'V', 'S',
                    'B', 'T', '2', 'n', 'A', 'C', 'e', 'Z', 'D', 'N', '_', 'k', '6', '7', 'U',
                    'u', 'a', 'J', 'K', 'b', 'j', 'r', 'E', 'I', '4', '5', 'v', 'w', 'O', 'P',
                    'W', 'o', '9' };

    char w1 = key[n % 63];
    char w3 = key[n / (63 * 63)];
    int i = (n - w1 - (n / (63 * 63)) * 63 * 63) / 63;
    char w2 = key[(n - n % 63 - (n / (63 * 63)) * 63 * 63) / 63];

    QString result;
    result.append(w3);
    result.append(w2);
    result.append(w1);
    return result;
}

QString VTranslate::encode_string63(QString s)
{
    QString result;
    foreach(QChar a, s)
    {
        result += dec_to_s63(a.unicode());
    }
    return "____" + result;
}

QString VTranslate::decode_string63(QString s)
{
    if(s.length() < 7 || (s.length()-4)%3!=0
            || s.mid(0, 4) != "____")
    {
        return s;
    }

    QString result;
    for(uint i=4; i<s.length(); i+=3)
    {
        result +=  s63_to_qchar(s.mid(i, 3));
    }
    return result;
}

QString VTranslate::decode(QString text)
{
    QRegularExpression reg;
    QRegularExpressionMatch match;

    reg.setPattern("____[a-zA-Z0-9_]+");
    match = reg.match(text);
    while(match.hasMatch())
    {
        QString dstring = VTranslate::decode_string63(match.captured());
        text = text.replace(match.capturedStart(), match.capturedLength(), dstring);
        match = reg.match(text, match.capturedStart() + dstring.length());
    }

    return text;
}

QString VTranslate::get_en_text(QString document)
{
    int start;
    init(document);

    QString result;
    while (_pos < _document_length)
    {
        QString block_str = next_block_str();
        uint length = block_str.length();
        if (length == 0)
        {
            continue;
        }


        if (length == 1)
        {
            if(_qutation_skip && (block_str == "\'" || block_str == "\""))
            {
                start = _pos;
                quotation_skip(block_str.at(0));
                result += block_str + _document.mid(start, _pos - start);
            }
            else if(_comment_skip &&( block_str == "*" && prefix_char() == '/'))
            {
                start = _pos;
                 multiple_line_comment_skip();
                result += block_str + _document.mid(start, _pos - start);
            }
            else if(_comment_skip && block_str == "/" && prefix_char() == '/')
            {
                start = _pos;
                line_comment_skip();
                result += block_str + _document.mid(start, _pos - start);
            }
            else
            {
                result += get_en_word(block_str);
            }
        }
        else
        {
            result += get_en_word(block_str);
        }
    }
    return result;
}

QString VTranslate::get_cn_text(QString document)
{
    int start;
    init(document);

    QString result;
    while (_pos < _document_length)
    {
        QString block_str = next_block_str();
        uint length = block_str.length();
        if (length == 0)
        {
            continue;
        }


        if (length == 1)
        {
            if(_qutation_skip && (block_str == "\'" || block_str == "\""))
            {
                start = _pos;
                quotation_skip(block_str.at(0));
                result += block_str + _document.mid(start, _pos - start);
            }
            else if(_comment_skip && (block_str == "*" && prefix_char() == '/'))
            {
                start = _pos;
                multiple_line_comment_skip();
                result += block_str + _document.mid(start, _pos - start);
            }
            else if(_comment_skip && block_str == "/" && prefix_char() == '/')
            {
                start = _pos;
                line_comment_skip();
                result += block_str + _document.mid(start, _pos - start);
            }
            else
            {
                result += get_cn_word(block_str);
            }
        }
        else
        {
            result += get_cn_word(block_str);
        }
    }
    return result;
}

void VTranslate::en_cn_map_clear()
{
    _en_cn_map.clear();
}

void VTranslate::cn_en_map_clear()
{
    _cn_en_map.clear();
}

void VTranslate::en_cn_map_append(QString key, QString value)
{
    _en_cn_map.insert(key, value);
}

void VTranslate::cn_en_map_append(QString key, QString value)
{
    _cn_en_map.insert(key, value);
}

void VTranslate::en_cn_map_append_keyword()
{
    foreach(QString key, VGlobal::keyword_en_cn.keys())
    {
        _en_cn_map.insert(key, VGlobal::keyword_en_cn.value(key));
    }
}

void VTranslate::cn_en_map_append_keyword()
{
    foreach(QString key, VGlobal::keyword_cn_en.keys())
    {
        _cn_en_map.insert(key, VGlobal::keyword_cn_en.value(key));
    }
}

void VTranslate::set_remap()
{
    foreach(QString key, _cn_en_map.keys())
    {
        _en_cn_map.insert(_cn_en_map.value(key), key);
    }
}

bool VTranslate::en_cn_map_contains(QString key)
{
    return _en_cn_map.contains(key);
}

bool VTranslate::cn_en_map_contains(QString key)
{
    return _cn_en_map.contains(key);
}

void VTranslate::set_qutation_skip(bool enable)
{
    _qutation_skip = enable;
}

void VTranslate::set_comment_skip(bool enable)
{
    _comment_skip = enable;
}

void VTranslate::init(QString text)
{
    _pos = 0;
    _document = text;
    _document_length = text.length();
}

QString VTranslate::next_block_str()
{
    if (_pos > _document_length)
    {
        return "";
    }

    QString result = "";
    for (size_t i = _pos; i < _document_length; i++)
    {
        if (is_legal_char(_document.at(i)))
        {
            result += _document.at(i);
        }
        else
        {
            if (result.length() == 0)
            {
                result = _document.at(i);
                _pos++;
                return result;
            }
            else
            {
                _pos = i;
                return result;
            }
        }
    }

    _pos = _document_length;
    return result;
}

void VTranslate::quotation_skip(QChar type)
{
    QString block_str = "";
    while (_pos < _document_length)
    {
        block_str = next_block_str();
        if (block_str.length() == 0)
        {
            continue;
        }
        if (block_str.at(0) == type)
        {
            if (prefix_char() != '\\')
            {
                return;
            }
        }
        else if (block_str.at(0) == '\r')
        {
            return;
        }
    }
}

void VTranslate::multiple_line_comment_skip()
{
    QString block_str = "";
    while (_pos < _document_length)
    {
        block_str = next_block_str();
        if (block_str.length() == 0)
        {
            continue;
        }
        if (block_str.at(0) == '/' && prefix_char() == '*')
        {
            return;
        }
    }
}

void VTranslate::line_comment_skip()
{
    QString block_str = "";
    while (_pos < _document_length)
    {
        block_str = next_block_str();
        if (block_str.length() == 0)
        {
            continue;
        }

        if (block_str.at(0) == '\r' || block_str.at(0) == '\n')
        {
            return;
        }
    }
}

QString VTranslate::get_en_word(QString word)
{
    // 如果map已经有这个word了
    // 直接取值返回
    if(_cn_en_map.contains(word))
    {
        return _cn_en_map.value(word);
    }

    QString result;
    if(VFunctions::is_chinese(word))
    {
        result = VTranslate::encode_string63(word);
    }
    else
    {
        result = word;
    }

//    QString result;
//    if(VFunctions::is_chinese(word))
//    {
//        if(VGlobal::keyword_cn_en.contains(word))
//        {
//            result = VGlobal::keyword_cn_en.value(word);
//        }
//        else
//        {

//            result = "codeglz" + QString::number(++_count);
////            qDebug() << this << "word: " << word << "to :" <<  result;
//        }
//        _cn_en_map.insert(word, result);
//    }
//    else
//    {
//        result = word;
//    }
    return result;
}

QString VTranslate::get_cn_word(QString word)
{
    QString result;
    if(_en_cn_map.contains(word))
    {
        result = _en_cn_map.value(word);
    }
    else
    {
        result = decode_string63(word);
    }
    return result;
}

QChar VTranslate::prefix_char()
{
    if (_pos >= 2)
    {
        return _document.at(_pos - 2);
    }
    return QChar();
}

bool VTranslate::is_legal_char(QChar word)
{
    int unicode = word.unicode();
    // _ 下划线 ascall 95
    if (unicode > 127 || word.isDigit() || word.isLetter() || unicode == 95)
    {
        return true;
    }
    return false;
}
